<template lang="pug">
dialogModel(
    ref="dialogLoad"
    :title="title"
    width="660"
    @confirm="confirm()"
)
    .cont-wrap
        uploadModel(
            :pop="accept?'支持'+accept+'文件':''"
            :accept="accept"
            :limit="limit"
            @onChange="onChange"
        )
    </template>

<script setup>
import { ref, onMounted } from 'vue'
import dialogModel from '@/components/dialogModel.vue'
import uploadModel from '@/components/uploadModel.vue'
import apis from '@/apis'
import { success } from '@/utils/message'
import { useRouter, useRoute } from 'vue-router'
import { storageGet } from '@/utils'
const route = useRoute()
const props = defineProps({
    type: {
        type: String,
        default: '主工程'
    },
    belongStage: {
        type: String,
        default: '0'
    },
    title: {
        type: String,
        default: '文件上传'
    }
})
const accept = ref('')
const dialogLoad = ref(null)
let info = {}
let upType = ref('')
let limit = ref(99)
const open = (data, type) =>{
    upType.value = type
    accept.value = ''
    fileList.value = []
    limit.value = 99
    if(type == '批量上传'){
        accept.value = '.zip'
        limit.value = 1
    }
    info = data
    dialogLoad.value.open()
}
const fileList = ref([])
const onChange = (file, list) =>{
    fileList.value = list
}

const emit = defineEmits(['updateSuccess']) //

let num = 0//计数
const confirm = () => {
    if (fileList.value.length === 0){
        warning('请选择上传文件！')
        return
    }
    if(upType.value == '批量上传'){
        uploadZip()
    }else{
        num = 0
        fileList.value.forEach(file => {
            cyFileuploadFile(file)
        })

    }
}
const cyFileuploadFile = (file) => {//
    apis.cyFileuploadFile({
        nodeId: info.id,
        file
    }).then((res) => {
        if(res.status == 1){
            success(res.msg)
            num++
            if(num == fileList.value.length){
                dialogLoad.value.close()
                emit('updateSuccess')
            }
        }
    })
}
const uploadZip = () => {//压缩包上传
    apis.uploadZip({
        file: fileList.value[0]
    }).then((res) => {
        if(res.status == 1){
            success(res.msg)
            dialogLoad.value.close()
            emit('updateSuccess', 'zip')
        }
    })
}

defineExpose({
    open
})

</script>

<style lang="less" scoped>
.cont-wrap{
    margin-top:20px;
    min-height: 220px;
    border-bottom: 1px solid #ccc;
}
</style>